#include"Stack.h"
zgw::Stack::Stack()
{
	_a = (int*)malloc(sizeof(int) * 4);
	_top = 0;
	_capacity = 4;
	_size = 0;
}

zgw::Stack::~Stack()
{
	free(_a);
	_a = NULL;
	_size = 0;
	_capacity = 0;
}

void zgw::Stack::Push(int x)
{	
	assert(_a != nullptr);
	if (_size == _capacity)
	{
		_a = (int*)realloc(_a, sizeof(int) * (_capacity * 2));
		if (_a == NULL)
		{
			perror("realloc fail:");
		}
		_capacity = 2 * _capacity;
	}
	_a[_size++] = x;
	_top += 1;
}

void zgw::Stack::Print()
{
	assert(_a != nullptr);
	assert(_size > 0);
	for (int i = 0; i < _size; i++)
	{
		cout << _a[i] << " ";
	}
	cout << endl;
}

void zgw::Stack::Pop()
{
	assert(_size > 0);
	_size--;
	_top--;
}

bool zgw::Stack::Empty()
{
	return _size == 0;
}

int zgw::Stack::Top()
{
	assert(_size> 0);
	return _a[_top - 1];
}

